﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace X3DLib
{
    public class Tree : X3dInterfaceObject
    {
        private Group group;
        private System.Collections.Generic.List<object> indexedFaceSet;
        private Transform transform;
        private IndexedLineSet indexedLineSet;
        private Background background;

        private int demoIndex = 1;//树木模型

        /// <summary>
        /// 选择树木模型的序号（1或2）
        /// </summary>
        private int DemoIndex
        {
            get { return demoIndex; }
            set { demoIndex = value; }
        }
        /// <summary>
        /// 场景的背景
        /// </summary>
        private Background Background
        {
            get { return background; }
            set { background = value; }
        }
        /// <summary>
        /// 树木模型1的树干
        /// </summary>
        private IndexedLineSet IndexedLineSet
        {
            get { return indexedLineSet; }
            set { indexedLineSet = value; }
        }

        private Transform Transform
        {
            get { return transform; }
            set { transform = value; }
        }
        /// <summary>
        /// 设置树叶形状
        /// </summary>
        private System.Collections.Generic.List<Object> IndexedFaceSet
        {
            get { return indexedFaceSet; }
            set { indexedFaceSet = value; }
        }

        private Group Group
        {
            get { return group; }
            set { group = value; }
        }

        private System.Collections.Generic.List<Object> appearance;

        private System.Collections.Generic.List<Object> Appearance
        {
            get { return appearance; }
            set { appearance = value; }
        }

        private System.Collections.Generic.List<Object> shape;

        private System.Collections.Generic.List<Object> Shape
        {
            get { return shape; }
            set { shape = value; }
        }

        private System.Collections.Generic.List<Object> coordinate;

        private System.Collections.Generic.List<Object> Coordinate
        {
            get { return coordinate; }
            set { coordinate = value; }
        }
        
        private Scene scene;

        public Scene Scene
        {
            get { return scene; }
            set { scene = value; }
        }
        /// <summary>
        /// 获得树木模型的基础模型，已过时（不推荐使用这个）
        /// </summary>
        /// <returns>Group</returns>
        public static Group getTreeGroup()
        {
            Group group = new Group();
            group.DEF = "group0";

            Background background = new Background();
            group.addChildren(background);
            //第一片叶子
            Shape shape = new Shape();
            shape.DEF = "L0";
            Coordinate coordinate = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
            IndexedFaceSet indexedFaceSet = new IndexedFaceSet();
            indexedFaceSet.coordIndex = "0 1 2 3";
            indexedFaceSet.Coordinate = coordinate;

            Appearance appearance = new Appearance();
            appearance.material = new Material("0 1 0");

            shape.gemeotry = indexedFaceSet;
            shape.appearance = appearance;
            group.addChildren(shape);
            //第二片叶子
            Transform transform = new Transform();
            transform.rotation = "0 1 0 3.14";
            transform.translation = "0 1 0";
            transform.addChildren(new Shape("L0"));
            group.addChildren(transform);
            //树干
            Shape shape2 = new Shape();
            shape2.DEF = "";
            IndexedLineSet indexedLineSet = new IndexedLineSet();
            indexedLineSet.coordIndex = "0 1";
            indexedLineSet.Coordinate = new Coordinate("0 0 0 0 2 0");

            Appearance app2 = new Appearance();
            app2.material.emissiveColor = "0.5 0.5 0";
            shape2.appearance = app2;
            shape2.gemeotry = indexedLineSet;
            group.addChildren(shape2);
            return group;
        }
        /// <summary>
        ///  获得树木迭代后生成的场景，已过时（不推荐使用这个）
        /// </summary>
        /// <param name="group0"></param>
        /// <param name="deep"></param>
        /// <returns></returns>
        public static Scene getTreeSence(Group group0, int deep)
        {
            Scene sence = new Scene();
            sence.addChildren(group0);

            for (int i = 1; i <= deep; i++)
            {
                Group tempgroup = new Group();
                tempgroup.DEF = "group" + i;
                Transform[] transform = new Transform[5];
                transform[0] = new Transform();
                transform[0].DEF = "s" + i;
                transform[0].scale = "0.5 0.5 0.5";
                transform[0].addChildren(new Group("group" + (i - 1)));

                transform[1] = new Transform();
                transform[1].translation = "0 1 0";
                transform[1].addChildren(new Transform("s" + i));

                transform[2] = new Transform();
                transform[2].DEF = "g" + i;
                transform[2].rotation = "1 0 0 0.75";
                transform[2].translation = "0 1.7 0";
                transform[2].addChildren(new Transform("s" + i));

                transform[3] = new Transform();
                transform[3].rotation = "0 1 0 2.09";
                transform[3].addChildren(new Transform("g" + i));

                transform[4] = new Transform();
                transform[4].rotation = "0 1 0 -2.09";
                transform[4].addChildren(new Transform("g" + i));

                foreach (Transform temp in transform)
                {
                    tempgroup.addChildren(temp);
                }

                sence.addChildren(tempgroup);

            }
            return sence;
        }
        /// <summary>
        /// 构造函数，默认使用第一个模型，迭代1次。
        /// </summary>
        public Tree()
        {
            //setScene(setGroup(1,"0 1 0","2"),1);
            this.addChildren(setGroup(demoIndex, "0 1 0", "2"));
            Group[] groupArr = new Group[1];
            groupArr = setGroupArr(1);
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }

            //scene = new Scene();

            //group = new Group();
            //group.DEF = "group0";

            //background = new Background();
            //group.addChildren(background);
            ////第一片叶子
            //shape = new List<Object>();
            //Shape tempShape1 = new Shape();
            //tempShape1.DEF = "L0";
            //shape.Add(tempShape1);
            ////shape.ElementAt(0).DEF = "L0";
            //coordinate = new List<Object>();
            //Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
            //coordinate.Add(tempCoor1);
            //IndexedFaceSet indexedFaceSet = new IndexedFaceSet();
            //indexedFaceSet.coordIndex = "0 1 2 3";
            //indexedFaceSet.Coordinate = tempCoor1;

            //appearance = new List<Object> ();
            //Appearance tempApp1 = new Appearance();
            //appearance.Add(tempApp1);
            //tempApp1.material = new Material("0 1 0");

            //tempShape1.gemeotry = indexedFaceSet;
            //tempShape1.appearance = tempApp1;
            //group.addChildren(tempShape1);
            ////第二片叶子
            //transform = new Transform();
            //transform.rotation = "0 1 0 3.14";
            //transform.translation = "0 1 0";
            //transform.addChildren(new Shape(tempShape1.DEF));
            //group.addChildren(transform);
            ////树干
            //Shape tempShape2 = new Shape();
            //shape.Add(tempShape2);
            //tempShape2.DEF = "";
            //IndexedLineSet indexedLineSet = new IndexedLineSet();
            //indexedLineSet.coordIndex = "0 1";
            //Coordinate tempCoor2 = new Coordinate("0 0 0 0 2 0");
            //coordinate.Add(tempCoor2);
            //indexedLineSet.Coordinate = tempCoor2;

            //Appearance tempApp2 = new Appearance();
            //appearance.Add(tempApp2);
            //tempApp2.material.emissiveColor = "0.5 0.5 0";
            //tempShape2.appearance = tempApp2;
            //tempShape2.gemeotry = indexedLineSet;
            //group.addChildren(tempShape2);

            //scene.addChildren(group);

            //for (int i = 1; i <= 1; i++)
            //{
            //    Group tempgroup = new Group();
            //    tempgroup.DEF = "group" + i;
            //    Transform[] transformArr = new Transform[5];
            //    transformArr[0] = new Transform();
            //    transformArr[0].DEF = "s" + i;
            //    transformArr[0].scale = "0.5 0.5 0.5";
            //    transformArr[0].addChildren(new Group("group" + (i - 1)));

            //    transformArr[1] = new Transform();
            //    transformArr[1].translation = "0 1 0";
            //    transformArr[1].addChildren(new Transform("s" + i));

            //    transformArr[2] = new Transform();
            //    transformArr[2].DEF = "g" + i;
            //    transformArr[2].rotation = "1 0 0 0.75";
            //    transformArr[2].translation = "0 1.7 0";
            //    transformArr[2].addChildren(new Transform("s" + i));

            //    transformArr[3] = new Transform();
            //    transformArr[3].rotation = "0 1 0 2.09";
            //    transformArr[3].addChildren(new Transform("g" + i));

            //    transformArr[4] = new Transform();
            //    transformArr[4].rotation = "0 1 0 -2.09";
            //    transformArr[4].addChildren(new Transform("g" + i));

            //    foreach (Transform temp in transformArr)
            //    {
            //        tempgroup.addChildren(temp);
            //    }

            //    scene.addChildren(tempgroup);

            //}

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="demoIndex">指定树木模型序号</param>
        /// <param name="deep">指定迭代深度</param>
        public Tree(int demoIndex,int deep)
        {
            //setScene(setGroup(demoIndex, "0 1 0", "2"), deep);
            this.demoIndex = demoIndex;
            this.addChildren(setGroup(demoIndex, "0 1 0", "2"));
            Group[] groupArr = new Group[deep];
            groupArr = setGroupArr(deep);
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="demoIndex">指定树木模型序号</param>
        /// <param name="color">颜色</param>
        /// <param name="size">大小（高度）</param>
        /// <param name="deep">指定迭代深度</param>
        public Tree(int demoIndex,string color,string size,int deep)
        {
            //setScene(setGroup(demoIndex, color, size), deep);
            this.demoIndex = demoIndex;
            this.addChildren(setGroup(demoIndex, color, size));
            Group[] groupArr = new Group[deep];
            groupArr = setGroupArr(deep);
            foreach (Group tempgroup in groupArr)
            {
                this.addChildren(tempgroup);
            }
        }
        /// <summary>
        /// 设置树木的基础模型
        /// </summary>
        /// <param name="demoIndex">指定树木模型序号</param>
        /// <param name="color">颜色</param>
        /// <param name="size">大小（高度）</param>
        /// <returns>Group</returns>
        public Group setGroup(int demoIndex, string color, string size)
        {
            group = new Group();
            group.DEF = "group0";

            Group demoGroup = new Group();
            demoGroup.DEF = "demo";

            background = new Background();
            group.addChildren(background);
            #region TreeDemo 1
            if (demoIndex == 1)
            {
                //第一片叶子
                shape = new List<Object>();
                Shape tempShape1 = new Shape();
                tempShape1.DEF = "L0";
                shape.Add(tempShape1);
                //shape.ElementAt(0).DEF = "L0";
                coordinate = new List<Object>();
                Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
                coordinate.Add(tempCoor1);

                indexedFaceSet = new List<object>();
                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetLeaf);
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                appearance = new List<Object>();
                Appearance tempApp1 = new Appearance();
                appearance.Add(tempApp1);
                tempApp1.material = new Material(color);//color="0 1 0"

                tempShape1.gemeotry = faceSetLeaf;
                tempShape1.appearance = tempApp1;
                group.addChildren(tempShape1);
                //第二片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                transform.translation = "0 1 0";
                transform.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transform);
                //树干
                Shape tempShape2 = new Shape();
                shape.Add(tempShape2);
                tempShape2.DEF = "";
                indexedLineSet = new IndexedLineSet();
                indexedLineSet.coordIndex = "0 1";
                Coordinate tempCoor2 = new Coordinate("0 0 0 0 "+size+" 0");//size="0 0 0 0 2 0"
                coordinate.Add(tempCoor2);
                indexedLineSet.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                appearance.Add(tempApp2);
                tempApp2.material.emissiveColor = "0.5 0.5 0";
                tempShape2.appearance = tempApp2;
                tempShape2.gemeotry = indexedLineSet;
                group.addChildren(tempShape2);
            }
            #endregion
            #region TreeDemo 2
            else if (demoIndex == 2)
            {
                //第一片叶子
                shape = new List<Object>();
                Shape tempShape1 = new Shape();
                tempShape1.DEF = "L0";
                shape.Add(tempShape1);
                //shape.ElementAt(0).DEF = "L0";
                coordinate = new List<Object>();
                Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
                coordinate.Add(tempCoor1);

                indexedFaceSet = new List<object>();
                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetLeaf);
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                appearance = new List<Object>();
                Appearance tempApp1 = new Appearance();
                appearance.Add(tempApp1);
                tempApp1.material = new Material(color);//color="0 1 0"

                tempShape1.gemeotry = faceSetLeaf;
                tempShape1.appearance = tempApp1;
                group.addChildren(tempShape1);
                //第二片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                transform.translation = "0 0 0";
                transform.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transform);
                //第三片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 1.57";
                transform.translation = "0 0 0";
                transform.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transform);

                //树干
                Shape tempShape2 = new Shape();
                shape.Add(tempShape2);
                tempShape2.DEF = "";
                indexedLineSet = new IndexedLineSet();
                indexedLineSet.coordIndex = "0 1";
                Coordinate tempCoor2 = new Coordinate("0 0 0 0 " + size + " 0");//size="0 0 0 0 2 0"
                coordinate.Add(tempCoor2);
                indexedLineSet.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                appearance.Add(tempApp2);
                tempApp2.material.emissiveColor = "0.5 0.5 0";
                tempShape2.appearance = tempApp2;
                tempShape2.gemeotry = indexedLineSet;
                group.addChildren(tempShape2);
            }
            #endregion
            #region TreeDemo 3
            else if (demoIndex == 3)
            {
                //第一片叶子
                shape = new List<Object>();
                Shape tempShape1 = new Shape();
                tempShape1.DEF = "L0";
                shape.Add(tempShape1);
                //shape.ElementAt(0).DEF = "L0";
                coordinate = new List<Object>();
                Coordinate tempCoor1 = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
                coordinate.Add(tempCoor1);

                indexedFaceSet = new List<object>();
                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetLeaf);
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                appearance = new List<Object>();
                Appearance tempApp1 = new Appearance();
                appearance.Add(tempApp1);
                tempApp1.material = new Material(color);//color="0 1 0"

                tempShape1.gemeotry = faceSetLeaf;
                tempShape1.appearance = tempApp1;
                group.addChildren(tempShape1);
                //第二片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                transform.translation = "0 2 0";
                transform.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transform);
                //第三片叶子
                Transform transformL2 = new Transform();
                transformL2.rotation = "0 1 0 1.57";
                transformL2.translation = "0 2 0";
                transformL2.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transformL2);
                //第四片叶子
                Transform transformL3 = new Transform();
                transformL3.rotation = "0 1 0 3.14";
                transformL3.scale = "0.5 0.5 0.5";
                transformL3.translation = "-0.7 1 -0.38";
                transformL3.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transformL3);
                //第5片叶子
                Transform transformL4 = new Transform();
                transformL4.rotation = "0 1 0 1.57";
                transformL4.scale = "0.5 0.5 0.5";
                transformL4.translation = "-0.7 1 -0.38";
                transformL4.addChildren(new Shape(tempShape1.DEF));
                group.addChildren(transformL4);
                //树干
                Shape tempShape2 = new Shape();
                shape.Add(tempShape2);
                tempShape2.DEF = "Banch";
                
                IndexedFaceSet faceSetBanch = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetBanch);
                faceSetBanch.DEF = "Trunk-FACES";
                faceSetBanch.ccw = "true";
                faceSetBanch.coordIndex = "0 1 9 8 -1,1 2 10 9 -1,2 3 11 10 -1,3 4 12 11 -1," +
                     "  4 5 13 12 -1,5 6 14 13 -1,6 7 15 14 -1,7 0 8 15 -1," +
                     "  0 1 2 3 4 5 6 7 -1,8 9 10 11 12 13 14 15";
                Coordinate tempCoor2 = new Coordinate();//size="0 0 0 0 2 0"
                tempCoor2.DEF = "Trunk-COORD";
                tempCoor2.point = "0.1 0 0.2 0.2 0 0.1 0.2 0 -0.1 0.1 0 -0.2 -0.1 0 -0.2 -0.2 0 -0.1 -0.2 0 0.1" +
                     "  -0.1 0 0.2 0.05 " + size + " 0.1 0.1 " + size + " 0.05 0.1 " + size + " -0.05 0.05 " + size + " -0.1 -0.05 " + size + " -0.1" +
                     "  -0.1 " + size + " -0.05 -0.1 " + size + " 0.05 -0.05 " + size + " 0.1";
                coordinate.Add(tempCoor2);
                faceSetBanch.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                appearance.Add(tempApp2);
                tempApp2.material.ambientIntensity = "1";
                tempApp2.material.diffuseColor = "0.5 0.4 0.3";
                tempApp2.material.shininess = "0.525";
                tempApp2.material.specularColor="0 0 0";
                tempApp2.material.transparency = "0";
                tempShape2.appearance = tempApp2;
                tempShape2.gemeotry = faceSetBanch;
                group.addChildren(tempShape2);

                //树干分支
                Transform transformB1 = new Transform();
                transformB1.rotation = "-1 1 0.5 1.57";
                transformB1.scale = "0.5 0.5 0.5";
                transformB1.translation = "0 1 0";
                transformB1.addChildren(new Shape(tempShape2.DEF));
                group.addChildren(transformB1);
                //树干分支
                Transform transformB2 = new Transform();
                transformB2.rotation = "1 1 0.5 -1.57";
                transformB2.scale = "0.5 0.5 0.5";
                transformB2.translation = "0 2 0";
                transformB2.addChildren(new Shape(tempShape2.DEF));
                group.addChildren(transformB2);
            }
            #endregion
            #region TreeDemo 4
            else if (demoIndex == 4)
            {
                //第一片叶子
                shape = new List<Object>();
                Shape tempShape1 = new Shape();
                tempShape1.DEF = "L0";
                shape.Add(tempShape1);
                //shape.ElementAt(0).DEF = "L0";
                coordinate = new List<Object>();
                Coordinate tempCoor1 = new Coordinate("0 3 0 0.4 3.6 0.2 0.8 4.2 0 0.2 3.6 -0.2");
                coordinate.Add(tempCoor1);

                indexedFaceSet = new List<object>();
                IndexedFaceSet faceSetLeaf = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetLeaf);
                faceSetLeaf.coordIndex = "0 1 2 3";
                faceSetLeaf.solid = "false";
                faceSetLeaf.Coordinate = tempCoor1;

                appearance = new List<Object>();
                Appearance tempApp1 = new Appearance();
                appearance.Add(tempApp1);
                tempApp1.material = new Material(color);//color="0 1 0"

                tempShape1.gemeotry = faceSetLeaf;
                tempShape1.appearance = tempApp1;
                demoGroup.addChildren(tempShape1);
                //第二片叶子
                transform = new Transform();
                transform.rotation = "0 1 0 3.14";
                //transform.translation = "0 2 0";
                transform.addChildren(new Shape(tempShape1.DEF));
                demoGroup.addChildren(transform);
                //第三片叶子
                Transform transformL2 = new Transform();
                transformL2.rotation = "0 1 0 1.57";
                //transformL2.translation = "0 2 0";
                transformL2.addChildren(new Shape(tempShape1.DEF));
                demoGroup.addChildren(transformL2);
                //第四片叶子
                Transform transformL3 = new Transform();
                transformL3.rotation = "0 1 0 3.14";
                transformL3.scale = "0.5 0.5 0.5";
                transformL3.translation = "-0.7 1 -0.38";
                transformL3.addChildren(new Shape(tempShape1.DEF));
                demoGroup.addChildren(transformL3);
                //第5片叶子
                Transform transformL4 = new Transform();
                transformL4.rotation = "0 1 0 1.57";
                transformL4.scale = "0.5 0.5 0.5";
                transformL4.translation = "-0.7 1 -0.38";
                transformL4.addChildren(new Shape(tempShape1.DEF));
                demoGroup.addChildren(transformL4);
                //树干
                Shape tempShape2 = new Shape();
                shape.Add(tempShape2);
                tempShape2.DEF = "Banch";

                IndexedFaceSet faceSetBanch = new IndexedFaceSet();
                indexedFaceSet.Add(faceSetBanch);
                faceSetBanch.DEF = "Trunk-FACES";
                faceSetBanch.ccw = "true";
                faceSetBanch.coordIndex = "0 1 9 8 -1,1 2 10 9 -1,2 3 11 10 -1,3 4 12 11 -1," +
                     "  4 5 13 12 -1,5 6 14 13 -1,6 7 15 14 -1,7 0 8 15 -1," +
                     "  0 1 2 3 4 5 6 7 -1,8 9 10 11 12 13 14 15";
                Coordinate tempCoor2 = new Coordinate();//size="0 0 0 0 2 0"
                tempCoor2.DEF = "Trunk-COORD";
                tempCoor2.point = "0.1 0 0.2 0.2 0 0.1 0.2 0 -0.1 0.1 0 -0.2 -0.1 0 -0.2 -0.2 0 -0.1 -0.2 0 0.1" +
                     "  -0.1 0 0.2 0.05 " + size + " 0.1 0.1 " + size + " 0.05 0.1 " + size + " -0.05 0.05 " + size + " -0.1 -0.05 " + size + " -0.1" +
                     "  -0.1 " + size + " -0.05 -0.1 " + size + " 0.05 -0.05 " + size + " 0.1";
                coordinate.Add(tempCoor2);
                faceSetBanch.Coordinate = tempCoor2;

                Appearance tempApp2 = new Appearance();
                appearance.Add(tempApp2);
                tempApp2.material.ambientIntensity = "1";
                tempApp2.material.diffuseColor = "0.5 0.4 0.3";
                tempApp2.material.shininess = "0.525";
                tempApp2.material.specularColor = "0 0 0";
                tempApp2.material.transparency = "0";
                tempShape2.appearance = tempApp2;
                tempShape2.gemeotry = faceSetBanch;
                demoGroup.addChildren(tempShape2);

                //树干分支
                Transform transformB1 = new Transform();
                transformB1.rotation = "-1 1 0.5 1.57";
                transformB1.scale = "0.5 0.5 0.5";
                transformB1.translation = "0 1 0";
                transformB1.addChildren(new Group(demoGroup.DEF));
                demoGroup.addChildren(transformB1);
                //树干分支
                Transform transformB2 = new Transform();
                transformB2.rotation = "1 1 0.5 -1.57";
                transformB2.scale = "0.5 0.5 0.5";
                transformB2.translation = "0 2 0";
                transformB2.addChildren(new Group(demoGroup.DEF));
                demoGroup.addChildren(transformB2);

                group.addChildren(demoGroup);
            }
            #endregion
            else
            {
                group.addChildren(new Shape(new Appearance(new Material("0 1 0")),new Box() ));
            }
            return group;
        }
        /// <summary>
        /// 在第一个Group的基础上设置迭代后的树木Group
        /// </summary>
        /// <param name="deep">迭代次数</param>
        /// <returns>Group[]</returns>
        public Group[] setGroupArr(int deep)
        {
            Group[] groupArr = new Group[deep];

            for (int i = 0; i < deep; i++)
            {
                groupArr[i] = new Group();
                groupArr[i].DEF = "group" + (i+1);
                Transform[] transformArr = new Transform[5];
                transformArr[0] = new Transform();
                transformArr[0].DEF = "s" + (i + 1);
                transformArr[0].scale = "0.5 0.5 0.5";
                transformArr[0].addChildren(new Group("group" + i));

                transformArr[1] = new Transform();
                transformArr[1].translation = "0 1 0";
                transformArr[1].addChildren(new Transform("s" + (i + 1)));

                transformArr[2] = new Transform();
                transformArr[2].DEF = "g" + (i + 1);
                transformArr[2].rotation = "1 0 0 0.75";
                transformArr[2].translation = "0 1.7 0";// "0 1 0";// 
                transformArr[2].addChildren(new Transform("s" + (i+1)));

                transformArr[3] = new Transform();
                transformArr[3].rotation = "0 1 0 2.09";
                transformArr[3].addChildren(new Transform("g" + (i + 1)));

                transformArr[4] = new Transform();
                transformArr[4].rotation = "0 1 0 -2.09";
                transformArr[4].addChildren(new Transform("g" + (i + 1)));

                foreach (Transform temp in transformArr)
                {
                    groupArr[i].addChildren(temp);                    
                }
                //System.Windows.Forms.MessageBox.Show(groupArr[i].toString());
            }
            return groupArr;
        }
        /// <summary>
        /// 功能相当于setGroupArr，区别是把所有的Group添加到Scene并返回
        /// </summary>
        /// <param name="group"></param>
        /// <param name="deep"></param>
        /// <returns></returns>
        public Scene setScene(Group group,int deep)
        {
            scene = new Scene();
            scene.addChildren(group);

            for (int i = 1; i <= deep; i++)
            {
                Group tempgroup = new Group();
                tempgroup.DEF = "group" + i;
                Transform[] transformArr = new Transform[5];
                transformArr[0] = new Transform();
                transformArr[0].DEF = "s" + i;
                transformArr[0].scale = "0.5 0.5 0.5";
                transformArr[0].addChildren(new Group("group" + (i - 1)));

                transformArr[1] = new Transform();
                transformArr[1].translation = "0 1 0";
                transformArr[1].addChildren(new Transform("s" + i));

                transformArr[2] = new Transform();
                transformArr[2].DEF = "g" + i;
                transformArr[2].rotation = "1 0 0 0.75";
                transformArr[2].translation = "0 1.7 0";
                transformArr[2].addChildren(new Transform("s" + i));

                transformArr[3] = new Transform();
                transformArr[3].rotation = "0 1 0 2.09";
                transformArr[3].addChildren(new Transform("g" + i));

                transformArr[4] = new Transform();
                transformArr[4].rotation = "0 1 0 -2.09";
                transformArr[4].addChildren(new Transform("g" + i));

                foreach (Transform temp in transformArr)
                {
                    tempgroup.addChildren(temp);
                }

                scene.addChildren(tempgroup);               

            }
            return scene;
        }
        /// <summary>
        /// 获取树木属性，已过时，不推荐使用
        /// </summary>
        /// <returns></returns>
        public string getAttributes()
        {
            //获得Coordinate的point字符串
            string coorvalue = ((Coordinate)(this.coordinate.ElementAt(1))).point;
            //获得倒数第一与倒数第二个空格之间的长度
            int lenght = coorvalue.LastIndexOf(" ")-1 - coorvalue.LastIndexOf(" ", coorvalue.LastIndexOf(" ") -1);
            //System.Windows.Forms.MessageBox.Show(lenght.ToString()+" "+ coorvalue.LastIndexOf(" ", coorvalue.LastIndexOf(" ") -1).ToString());
            //获得影响树木高度的值
            string size = coorvalue.Substring(coorvalue.LastIndexOf(" ") - lenght, lenght);
            return "<Attributes>\n"
                    + "<Element " + "Name=\"" + this.appearance.ElementAt(1).GetType().Name 
                    + "\" value=\"" + ((Appearance)(this.appearance.ElementAt(0))).material.diffuseColor+ "\" />\n"
                    + "<Element " + "Name=\"" + this.coordinate.ElementAt(1).GetType().Name
                    + "\" value=\"" + size + "\" />\n"
                    + "</Attributes>\n";
        }
        /// <summary>
        /// 设置树木的属性，已过时，不推荐使用
        /// </summary>
        /// <param name="demoIndex">指定树木模型</param>
        /// <param name="color">树叶颜色</param>
        /// <param name="size">高度</param>
        public void setAttributes(int demoIndex, string color, string size)
        {
            this.demoIndex = demoIndex;
            //setGroup(demoIndex,color ,size);
            ((Appearance)(this.appearance.ElementAt(0))).material.diffuseColor = color;
            if (demoIndex == 1)
            {
                if (indexedLineSet == null)
                {
                    indexedLineSet = new IndexedLineSet();
                    indexedLineSet.coordIndex = "0 1";
                }                
                ((Coordinate)(this.coordinate.ElementAt(1))).point = "0 0 0 0 " + size + " 0";
            }
            else if (demoIndex == 2)
            {
                ((Coordinate)(this.coordinate.ElementAt(1))).point = "0.1 0 0.2 0.2 0 0.1 0.2 0 -0.1 0.1 0 -0.2 -0.1 0 -0.2 -0.2 0 -0.1 -0.2 0 0.1" +
                     "  -0.1 0 0.2 0.05 " + size + " 0.1 0.1 " + size + " 0.05 0.1 " + size + " -0.05 0.05 " + size + " -0.1 -0.05 " + size + " -0.1" +
                     "  -0.1 " + size + " -0.05 -0.1 " + size + " 0.05 -0.05 " + size + " 0.1";
            }
        }
        /// <summary>
        /// 获取树木属性
        /// </summary>
        /// <returns></returns>
        public override string getAttrs()
        {
            //获得Coordinate的point字符串
            string coorvalue = ((Coordinate)(this.coordinate.ElementAt(1))).point;
            //获得倒数第一与倒数第二个空格之间的长度
            int lenght = coorvalue.LastIndexOf(" ") - 1 - coorvalue.LastIndexOf(" ", coorvalue.LastIndexOf(" ") - 1);
            //System.Windows.Forms.MessageBox.Show(lenght.ToString()+" "+ coorvalue.LastIndexOf(" ", coorvalue.LastIndexOf(" ") -1).ToString());
            //获得影响树木高度的值
            string size = coorvalue.Substring(coorvalue.LastIndexOf(" ") - lenght, lenght);
            return  //"demoIndex|"+this.demoIndex.ToString()+"|max:2 min:1,"+
                    "color|" + ((Appearance)(this.appearance.ElementAt(0))).material.diffuseColor + "|max:1 min:0,"
                    + "size|" + size + "|float";
        }
        /// <summary>
        /// 设置数目属性
        /// </summary>
        /// <param name="attrs">属性字符串，格式ver|ver|ver,…</param>
        public override void setAttrs(string attrs)
        {
            //int demoIndex=1;
            string color="0 1 0";
            string size = "3";
            string[][] temp = X3DString.getArrayByAttrs(attrs);
            for (int i = 0; i < temp.Length; i++)
            {
                switch (temp[i][0])
                {
                    //case "demoIndex":
                    //    demoIndex = Int32.Parse( temp[i][1]);
                    //    break;
                    case "color":
                        color = temp[i][1];
                        break;
                    case "size":
                        size = temp[i][1];
                        break;
                }
            }
            setAttributes(this.demoIndex, color, size);
        }
        //获取、修改树木形状
        //
    }
}
